package logic

import (
	"context"
	"encoding/json"
	"github.com/zeromicro/go-zero/core/logx"
	"newuser/models"
	"newuser/newuserrpc/internal/svc"
	"newuser/newuserrpc/newuserrpc"
	"time"
)

type NewUserRpcListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNewUserRpcListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NewUserRpcListLogic {
	return &NewUserRpcListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 列表查询
func (l *NewUserRpcListLogic) NewUserRpcList(in *newuserrpc.NewUserRpcListRequest) (*newuserrpc.NewUserRpcListResponse, error) {
	// todo: add your logic here and delete this line

	var user []models.User
	get, _ := models.Rdb.Get(l.ctx, "user_list_").Result()
	if get == "" {
		models.DB.Debug().Find(&user)
		marshal, _ := json.Marshal(user)
		models.Rdb.Set(l.ctx, "user_list_", marshal, time.Hour*1)
	} else {
		json.Unmarshal([]byte(get), &user)
	}

	var userlist []*newuserrpc.NewUserList
	for _, i := range user {
		list := newuserrpc.NewUserList{
			Username: i.Username,
			Password: i.Password,
		}
		userlist = append(userlist, &list)
	}
	return &newuserrpc.NewUserRpcListResponse{
		List: userlist,
	}, nil
}
